package proconfig

import (
	"os"
	"path/filepath"
	"strings"

	beegoconfig "github.com/astaxie/beego/config"
)

var Config beegoconfig.Configer

func NewConfig(runMode, configPath string) (beegoconfig.Configer, error) {
	if runMode == "" {
		runMode = "prod"
	}

	fileName := filepath.Join(configPath, runMode, "config.ini")

	c, err := beegoconfig.NewConfig("ini", fileName)
	if err != nil {
		return nil, err
	} else {
		return c, nil
	}
}

func InitByRunMode(runMode string) {
	var err error

	workPath, _ := os.Getwd()
	workPath, _ = filepath.Abs(workPath)

	workPathList := strings.Split(workPath, string(os.PathSeparator))
	configPathList := workPathList[:len(workPathList)-1]
	configPathList = append(configPathList, "conf")
	configPath := strings.Join(configPathList, string(os.PathSeparator))

	if runMode == "" {
		runMode = "prod"
	}

	Config, err = NewConfig(runMode, configPath)

	if err != nil {
		//设置一个空数据，方便处理
		Config, _ = beegoconfig.NewConfigData("ini", []byte(""))
	}
}
